using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CareerSeasonLayer : MonoBehaviour
{
	public UITable table;

	public GameObject seasonIntervalPrefab;

	public List<SeasonIntervalDisplay> intervals = new List<SeasonIntervalDisplay>();

	public GameObject seasonEnd;

	public UIScrollView scrollView;

	private void Awake()
	{
		Init();
	}

	private void OnEnable()
	{
		Init();
	}

	public void Init()
	{
		TournamentCareerStats careerStatsManager = GGPlayerSettings.instance.careerStatsManager;
		SeasonDefinitions.SeasonId seasonId = (SeasonDefinitions.SeasonId)careerStatsManager.TournamentCareer.activeSeasonInterval.seasonId;
		SeasonDefinitions.SeasonIntervalId seasonIntervalId = (SeasonDefinitions.SeasonIntervalId)careerStatsManager.TournamentCareer.activeSeasonInterval.seasonIntervalId;
		SeasonDefinitions.Season season = SeasonDefinitions.instance.GetSeason(seasonId);
		foreach (SeasonDefinitions.SeasonInterval seasonInterval in season.seasonIntervals)
		{
			if (seasonInterval.intervalId == seasonIntervalId && seasonInterval.isPlayedAllEvents && season.GetNextInterval(seasonInterval) != null)
			{
				careerStatsManager.BeginSeasonInterval(season, season.GetNextInterval(seasonInterval));
				seasonIntervalId = seasonInterval.intervalId;
			}
		}
		int num = 0;
		UIWidget uIWidget = null;
		foreach (SeasonDefinitions.SeasonInterval seasonInterval2 in season.seasonIntervals)
		{
			if (num >= intervals.Count)
			{
				intervals.Add(NGUITools.AddChild(table.gameObject, seasonIntervalPrefab).GetComponent<SeasonIntervalDisplay>());
			}
			SeasonIntervalDisplay seasonIntervalDisplay = intervals[num];
			seasonIntervalDisplay.Init(seasonInterval2, this);
			if (seasonInterval2.intervalId == seasonIntervalId)
			{
				uIWidget = seasonIntervalDisplay.GetComponent<UIWidget>();
			}
			seasonIntervalDisplay.gameObject.name = GGFormat.IndexName(num);
			num++;
		}
		seasonEnd.name = GGFormat.IndexName(num);
		table.Reposition();
		if (uIWidget != null)
		{
			CenterWidgetOnScroll(uIWidget);
		}
	}

	protected void CenterWidgetOnScroll(UIWidget centeredWidget)
	{
		StopAllCoroutines();
		StartCoroutine(DoCenterWidgetOnScroll(centeredWidget));
	}

	protected IEnumerator DoCenterWidgetOnScroll(UIWidget centeredWidget)
	{
		for (int i = 0; i < 3; i++)
		{
			yield return null;
		}
		UITools.CenterWidgetOnScroll(centeredWidget, scrollView);
	}

	public void OnResetButtonPressed()
	{
		TournamentCareerStats careerStatsManager = GGPlayerSettings.instance.careerStatsManager;
		SeasonDefinitions.Season season = SeasonDefinitions.instance.GetSeason(SeasonDefinitions.SeasonId.Season1);
		careerStatsManager.BeginSeasonInterval(season, season.seasonIntervals[0]);
		Init();
	}

	public void OnNextSeasonButtonPressed()
	{
		TournamentCareerStats careerStatsManager = GGPlayerSettings.instance.careerStatsManager;
		careerStatsManager.AdvanceToNextSeason();
		Init();
	}
}
